package t10086.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import t10086.Model.Entity.DeliveryRecord;
import t10086.Repository.DeliveryRecordRepository;

import java.util.List;

@Service
public class DeliveryRecordService {

    private final DeliveryRecordRepository deliveryRecordRepository;
    @Autowired
    public DeliveryRecordService(DeliveryRecordRepository deliveryRecordRepository) {
            this.deliveryRecordRepository = deliveryRecordRepository;
    }
    public List<DeliveryRecord> getAll() {
        return deliveryRecordRepository.findAll();
    }

    public void deliveryFailed(String serialNumber) {
        this.deliveryRecordRepository.deliverFailed(serialNumber);
    }

    public void updateDeliveryRecord(String orderNumber,String serialNumber,int window,int status,int consumedTime) {
           this.deliveryRecordRepository.updateDeliverRecordStatus(orderNumber,serialNumber,status,window,consumedTime);
    }
    public List<DeliveryRecord> getDeliveryRecordsByStatus(int status) {
        return deliveryRecordRepository.findByStatus(status);
    }
    public List<DeliveryRecord> getDeliveryBySerialNumber(String serialNumber) {
        return deliveryRecordRepository.findBySerialNumber(serialNumber);
    }

    public List<DeliveryRecord> getDeliveryByOrderNumber(String orderNumber) {
        return deliveryRecordRepository.findByOrderNumber(orderNumber);
    }
    public List<DeliveryRecord> getDeliveryBySerialNumberAndOrderNumber(String serialNumber, String orderNumber) {
        return deliveryRecordRepository.findBySerialNumberAndOrderNumber(serialNumber, orderNumber);
    }
    public void save(DeliveryRecord deliveryRecord) {
        deliveryRecordRepository.save(deliveryRecord);
    }
    public boolean addDeliveryRecord(DeliveryRecord deliveryRecord) {
        try{
            deliveryRecordRepository.save(deliveryRecord);
        }catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }
}
